package tests;

import common.WebAutoTest.TestUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

/**
 * @Author: wenjingyuan
 * @Date: 2023/09/18/10:46
 * @Description:
 */
public class BlogEdit extends TestUtils {
    public static ChromeDriver driver=new ChromeDriver();
    @Test
    @BeforeAll
    static void init() {
        driver.get("http://121.40.90.244:8080/blog_edit.html?aid=9");
    }
    /**
     * 博客修改页
     * 输入标题和正文，点击“修改文章”
     * 是否正常跳转个人博客列表
     */

    //正常输入跳转
    @ParameterizedTest
    @CsvSource({"test_title, http://121.40.90.244:8080/myblog_list.html"})
    void blog_edit_success (String title,String myblog_list_url) throws InterruptedException {
//        login.login("111","123","http://120.27.224.221:8080/myblog_list.html");
        //到达博客修改页（修改id为9的文章）
        driver.get("http://121.40.90.244:8080/blog_edit.html?aid=9");
        //输入标题和正文
        driver.findElement(By.cssSelector("#title")).sendKeys(title);
        driver.findElement(By.cssSelector("#editorDiv > div.editormd-toolbar > div > ul > li:nth-child(21) > a > i")).click();
        //点击按钮提交
        driver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        Thread.sleep(100);//等待弹窗
        //处理弹窗
        Alert alert = driver.switchTo().alert();
        String expect_alert = "修改成功！";
        String actual_alert = alert.getText();
        Assertions.assertEquals(actual_alert,expect_alert);
        alert.accept();
        //跳转到列表页
        //获取到当前的url，使用断言如果等于要求的值->通过，否则不通过
        String cur_url = driver.getCurrentUrl();
        Assertions.assertEquals(myblog_list_url ,cur_url);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }
    //未登录状态下：直接跳转登录页
    @ParameterizedTest
    @ValueSource(strings = "http://121.40.90.244:8080/login.html")
    void blog_edit_faild(String login_url){
        //到达博客修改页
        driver.get("http://121.40.90.244:8080/blog_edit.html?aid=9");
        String cur_url = driver.getCurrentUrl();
        Assertions.assertEquals(login_url ,cur_url);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }
    @Test
    @AfterAll
    static void exit() {
        driver.quit();
    }

}
